﻿using System;
using System.Collections.Generic;
using System.Reflection;

namespace WindowsPhone7InputScopes
{
    public static class EnumHelper
    {
        /// <summary>
        /// Gets the string representation of an Enumeration.
        /// </summary>
        /// <remarks>
        /// This is based Mike Harsh's MiX10 video:
        /// http://live.visitmix.com/MIX10/Sessions/CL16
        /// </remarks>
        /// <param name="enumType">The Enum</param>
        /// <returns>A string representation of the enum.</returns>
        public static IEnumerable<string> GetNames(Type enumType)
        {
            if (!enumType.IsEnum)
                throw new InvalidOperationException("enumType is not an Enum.");

            FieldInfo[] fields = enumType.GetFields();
            IList<string> list = new List<string>(fields.Length);

            foreach (FieldInfo field in fields)
            {
                list.Add(field.Name);
            }
            return list;
        }

        public static IEnumerable<string> GetNames<T>() where T : struct
        {
            return GetNames(typeof (T));
        }

        public static T GetValue<T>(string value)
        {
            return (T) Enum.Parse(typeof (T), value, true);
        }
    }
}